package com.skytech.component.monitor.enterprise_data_monitoring;


import com.alibaba.fastjson.JSONObject;
import com.skytech.component.monitor.data.MonitorData;
import com.skytech.component.monitor.util.FormateConnection;
import com.skytech.component.monitor.util.SaveDataUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class FuJianService implements CrawlerScript {
    private static final Logger log = LoggerFactory.getLogger(FuJianService.class);
   

    private static final String province = "福建";

    @Autowired
    private SaveDataUtil saveDataUtil;

    @Override
    public void start() {
        for (int i = 220; i <= 229; i++) {
            String url = "http://wryfb.fjemc.org.cn/index.aspx?area_id=" + i;
            List<JSONObject> jsonObjectList = new ArrayList<>();
            try {
                Document document = FormateConnection.createConnection(Jsoup.connect(url))
                        .get();
                log.info("当前页面--{}", url);
                Elements allElements = document.getAllElements();
                Elements elements = allElements.select("body > div.wrap.content1 > div.fr.right > div > div.content.bottom10 > table > tbody tr:nth-child(n+2)");
                for (Element e : elements) {
                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("entName", e.getElementsByTag("td").get(1).text());
                    jsonObject.put("area", e.getElementsByTag("td").get(0).text());
                    jsonObject.put("industry", e.getElementsByTag("td").get(2).text());
                    jsonObject.put("href", "http://wryfb.fjemc.org.cn/" + e.getElementsByTag("a").attr("href"));
                    jsonObjectList.add(jsonObject);
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                continue;
            }
            detailData(jsonObjectList);

        }
        log.info("福建--数据爬取结束");

    }

    private void detailData(List<JSONObject> jsonObjectList) {
        for (JSONObject jsonObject : jsonObjectList) {
            String url = jsonObject.getString("href");
            try {
                Document document = FormateConnection.createConnection(Jsoup.connect(url))
                        .get();
                log.info("当前页面--{}", url);
                Elements allElements = document.getAllElements();
                String dataUrl = "http://wryfb.fjemc.org.cn/" + allElements.select("body > div.wrap.content1 > div.fl.left > div > div.content.bottom10 > ul > li:nth-child(10) > a").attr("href");
                jsonObject.put("dataUrl", dataUrl);
                listEnterpriseData(jsonObject);
            } catch (Exception e) {
                log.error(e.getMessage());
                continue;
            }
        }
    }

    static String monitorPoint = null;
    static String monitorTime = null;

    private void listEnterpriseData(JSONObject jsonObject) {
        String url = jsonObject.getString("dataUrl");

        List<MonitorData> monitorDataList = new ArrayList<>();
        try {
            Document document = FormateConnection.createConnection(Jsoup.connect(url))
                    .get();
            log.info("当前页面--{}", url);
            Elements allElements = document.getAllElements();
            Elements elements = allElements.select("#aspnetForm > div:nth-child(5) > table > tbody tr:nth-child(n+2)");
            if (!elements.isEmpty()) {

                for (Element e : elements) {

                    MonitorData monitorData = new MonitorData();
                    if (e.getElementsByTag("td").size() > 6) {
                        monitorPoint = e.getElementsByTag("td").get(0).text();
                        monitorTime = e.getElementsByTag("td").get(1).text();

                        monitorData.setId(UUID.randomUUID().toString());
                        monitorData.setProvince(province);
                        monitorData.setCompanyName(jsonObject.getString("entName"));
                        monitorData.setArea(jsonObject.getString("area"));
                        monitorData.setIndustry(jsonObject.getString("industry"));
                        monitorData.setMonitorPoint(monitorPoint);
                        monitorData.setMonitorTime(monitorTime);
                        monitorData.setMonitorInfo(e.getElementsByTag("td").get(2).text());
                        monitorData.setMonitorValue(e.getElementsByTag("td").get(3).text());
                        monitorData.setMonitorStandardValue(e.getElementsByTag("td").get(4).text());
                        monitorData.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                    } else {

                        monitorData.setId(UUID.randomUUID().toString());
                        monitorData.setProvince(province);
                        monitorData.setCompanyName(jsonObject.getString("entName"));
                        monitorData.setArea(jsonObject.getString("area"));
                        monitorData.setIndustry(jsonObject.getString("industry"));
                        monitorData.setMonitorPoint(monitorPoint);
                        monitorData.setMonitorTime(monitorTime);
                        monitorData.setMonitorInfo(e.getElementsByTag("td").get(0).text());
                        monitorData.setMonitorValue(e.getElementsByTag("td").get(1).text());
                        monitorData.setMonitorStandardValue(e.getElementsByTag("td").get(2).text());
                        monitorData.setOverproof(e.getElementsByTag("td").get(3).text());
                        monitorData.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    }
                    monitorDataList.add(monitorData);

                }
                saveDataUtil.saveTag(monitorDataList, province);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return;
        }


    }


}
